//
//  HWVipCenterController.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/9.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWVipCenterController: HWBaseViewController {
    var topBgView:UIView!
    var topImageView:UIImageView!
    var headBgView:UIView!
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var nameLabel:UILabel!
    /// 上涨
    var styleImageView:UIImageView!
    var styleExpLabel:UILabel!
    /// 升级剩余
    var upgradeSurplusLabel:UILabel!
    /// 开通按钮
    var openButton:UIButton!
    
    var progressBarBg:UIView!
    var progressBar:YLProgressBar!
    
    /// 开通提示
    var openTipsLabel:UILabel!
    
    var bottomBgView:UIView!
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    
    var httpManager = HWVIPHttpManager()
    
    var segmentTitles:[String] = Array()
    var vipCenterModel = HWVipCenterModel()
    var callBack:(()->())?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        loadData()
    }
    
    
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        UIColor.gradientColor(view: topBgView, colors: [UIColor.colorWithHexString(hexString: "#646468").cgColor,
                                                        UIColor.colorWithHexString(hexString: "#33333F").cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 1))
        
        bottomBgView.addCorner(conrners: [.topLeft,.topRight], radius: 29)
        //        
        UIColor.gradientColor(view: topImageView, colors: [UIColor.colorWithHexString(hexString: "#262626").cgColor,
                                                           UIColor.colorWithHexString(hexString: "#232323").cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 1))
        
//        UIColor.gradientColor(view: headBgView, colors: [UIColor.colorWithHexString(hexString: "#242424").cgColor,
//                                                         UIColor.colorWithAlphaHexString(hexString: "#FFFFFF", alpha: 0).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 0.5))
        
        UIColor.gradientColor(view: progressBarBg, colors: [UIColor.colorWithHexString(hexString: "#999999").cgColor,
                                                            UIColor.colorWithAlphaHexString(hexString: "#666666", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
    }
    
}

extension HWVipCenterController {
    private func initUI() -> Void {
        navigationView.backgroundColor = .clear
        navigationView.titleLabel.text = "VIP福利中心"
        navigationView.titleLabel.textColor = .white
        
        topBgView = UIView()
        view.addSubview(topBgView)
        view.sendSubviewToBack(topBgView)
        topImageView = UIImageView()
        //        topImageView.image = UIImage(named: "openVIP")
        topImageView.layer.cornerRadius = 29*widthScale
        topImageView.layer.masksToBounds = true
        topImageView.isUserInteractionEnabled = true
        view.addSubview(topImageView)
        
        headBgView = UIView()
        headBgView.layer.cornerRadius = 79/2*widthScale
        //        headBgView.backgroundColor = .blue
        headBgView.layer.masksToBounds = true
        headBgView.backgroundColor = .clear
        view.addSubview(headBgView)
        
        headImageView = UIImageView()
        headImageView.backgroundColor = .clear
        headImageView.layer.cornerRadius = 73/2*widthScale
        headImageView.layer.masksToBounds = true
        headImageView.contentMode = .scaleAspectFit
        headBgView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
//        headFrameImageView.layer.cornerRadius = 83/2*widthScale
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFit
        view.addSubview(headFrameImageView)
        
        nameLabel = HWLabel.createLabel(title: "尊贵的  用户：", color: "#FFFFFF", font: 12*widthScale)
        topImageView.addSubview(nameLabel)
        
        upgradeSurplusLabel = HWLabel.createLabel(title: "VIP值：0，距离下一级剩余:0", color: "#999999", font: 10*widthScale)
        topImageView.addSubview(upgradeSurplusLabel)
        
        styleImageView = UIImageView()
        topImageView.addSubview(styleImageView)
        
        styleExpLabel = HWLabel.createLabel(title: "", color: "#3BB958", font: 10*widthScale)
        topImageView.addSubview(styleExpLabel)
        
        openTipsLabel = HWLabel.createLabel(title: "开通VIP，可享受 VIP 福利中心所有礼包及特权", color: "#DAC38A", font: 10*widthScale)
        topImageView.addSubview(openTipsLabel)
        
        openButton = UIButton(type: .custom)
        openButton.setTitle("开通 VIP", for: .normal)
        openButton.titleLabel?.font = .systemFont(ofSize: 10*widthScale)
        openButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
        openButton.layer.cornerRadius = 8*widthScale
        openButton.backgroundColor = .colorWithHexString(hexString: "#DAC38A")
        openButton.addTarget(self, action: #selector(openButtonClick(_:)), for: .touchUpInside)
        topImageView.addSubview(openButton)
        
        progressBarBg = UIView()
        progressBarBg.layer.cornerRadius = 2*widthScale
        progressBarBg.layer.masksToBounds = true
        view.addSubview(progressBarBg)
        
        progressBar = YLProgressBar()
        progressBar.indicatorTextDisplayMode = .none
        progressBar.progressTintColors = [UIColor.colorWithHexString(hexString: "#585858"),UIColor.colorWithHexString(hexString: "#2A2A2A")]
        progressBar.trackTintColor = .clear
        progressBar.hideStripes = true
        progressBar.progress = 0
        progressBar.hideGloss = true
        progressBar.progressStretch = true
        progressBar.progressBarInset = 0
        view.addSubview(progressBar)
        
        bottomBgView = UIView()
        bottomBgView.backgroundColor = .colorWithHexString(hexString: "#F3F4F8")
        view.addSubview(bottomBgView)
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = []
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#999999")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleNormalFont = UIFont.init(name: "YouSheBiaoTiHei", size: 14*widthScale)!
        segmentedDataSource.titleSelectedFont = UIFont.init(name: "YouSheBiaoTiHei", size: 16*widthScale)!
        //        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 12)
        //        segmentedDataSource.titleSelectedFont = .boldSystemFont(ofSize: 14)
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorImageView()
        indicator.image = UIImage(named: "indicator")
        indicator.indicatorHeight = 7*widthScale
        indicator.indicatorWidth = 21*widthScale
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        segmentedView.backgroundColor = .colorWithHexString(hexString: "#F3F4F8")
        bottomBgView.addSubview(self.segmentedView)
        
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
    }
    
    private func initFrame() -> Void {
        topBgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(screenHeight/2)
        }
        
        topImageView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(navigationView.snp.bottom).offset(25*widthScale)
            make.height.equalTo(topImageView.snp.width).multipliedBy(0.38)
        }
        
        headBgView.snp.makeConstraints { make in
            make.width.height.equalTo(79*widthScale)
            make.top.equalTo(topImageView.snp.top).offset(-20*widthScale)
            make.right.equalTo(topImageView.snp.right).offset(-24*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(15*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(73*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.width.height.equalTo(108*widthScale)
        }
        
        openTipsLabel.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
        openButton.snp.makeConstraints { make in
            make.right.equalTo(-23*widthScale)
            make.bottom.equalTo(-10*widthScale)
            make.width.equalTo(50*widthScale)
            make.height.equalTo(16*widthScale)
        }
        
        progressBarBg.snp.makeConstraints { make in
            make.left.equalTo(35*widthScale)
            make.bottom.equalTo(openButton.snp.top).offset(-9*widthScale)
            make.height.equalTo(4*widthScale)
            make.right.equalTo(-35*widthScale)
        }
        
        progressBar.snp.makeConstraints { make in
            make.left.equalTo(35*widthScale)
            make.bottom.equalTo(openButton.snp.top).offset(-9*widthScale)
            make.height.equalTo(4*widthScale)
            make.right.equalTo(-35*widthScale)
        }
        
        upgradeSurplusLabel.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.bottom.equalTo(progressBar.snp.top).offset(-6*widthScale)
        }
        
        styleImageView.snp.makeConstraints { make in
            make.left.equalTo(upgradeSurplusLabel.snp.right).offset(15*widthScale)
            make.width.equalTo(5*widthScale)
            make.height.equalTo(10*widthScale)
            make.centerY.equalTo(upgradeSurplusLabel)
        }
        
        styleExpLabel.snp.makeConstraints { make in
            make.left.equalTo(styleImageView.snp.right).offset(2*widthScale)
            make.centerY.equalTo(styleImageView)
        }
        
        bottomBgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(topImageView.snp.bottom).offset(20*widthScale)
        }
        
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(15*widthScale)
            make.height.equalTo(30*widthScale)
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom).offset(10*widthScale)
            make.bottom.equalTo(0)
        }
        
    }
}

extension HWVipCenterController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if self.vipCenterModel.nav[index].type == 0 {
            let vipGiftBag = HWVipGiftBagController()
            vipGiftBag.type = self.vipCenterModel.nav[index].type
            return vipGiftBag
        }else if self.vipCenterModel.nav[index].type == 1 {
            let vipHeadProtraitFrame = HWVipHeadPortraitFrameController()
            vipHeadProtraitFrame.type = self.vipCenterModel.nav[index].type
            vipHeadProtraitFrame.callBack = {
                self.loadData()
            }
            return vipHeadProtraitFrame
        }else if self.vipCenterModel.nav[index].type == 2 {
            let nickName = HWVipColorfulNickNameController()
            nickName.type = self.vipCenterModel.nav[index].type
            return nickName
        }else if self.vipCenterModel.nav[index].type == 3 {
            let vipCoupons = HWVipGiftBagCouponsController()
            vipCoupons.type = self.vipCenterModel.nav[index].type
            return vipCoupons
        }else if self.vipCenterModel.nav[index].type == 4 {
            let vipMedal = HWVipMedalController()
            vipMedal.type = self.vipCenterModel.nav[index].type
            vipMedal.callBack = {
                self.callBack?()
            }
            return vipMedal
        }else {
            return HWVipGiftBagController()
        }
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}

extension HWVipCenterController {
    func loadData() {
        httpManager.getVIPCenterHomeInfo { model in
            self.vipCenterModel = model
            self.nameLabel.text = model.top.name
            self.upgradeSurplusLabel.text = "VIP值：\(model.top.exp ?? 0)，距离下一级剩余:\((model.top.last_exp ?? 0) - (model.top.exp ?? 0))"
            self.openTipsLabel.text = model.top.end_time
            self.progressBar.progress = CGFloat(model.top.exp) / CGFloat(model.top.last_exp)
            self.headImageView.sd_setImage(with: URL(string: model.top.head), completed: nil)
            self.headFrameImageView.sd_setImage(with: URL(string: model.top.head_frame), completed: nil)
            self.styleImageView.sd_setImage(with: URL(string: model.top.style_img), completed: nil)
            self.styleExpLabel.text = model.top.style_exp
            
            if model.top.style_exp.contains("+") == true {
                self.styleExpLabel.textColor = .colorWithHexString(hexString: "#3BB958")
            }else{
                self.styleExpLabel.textColor = .colorWithHexString(hexString: "#E51111")
            }
            self.segmentTitles.removeAll()
            for mod in model.nav {
                self.segmentTitles.append(mod.name)
            }
            if model.top.vip > 0 {
                self.openButton.setTitle("续费", for: .normal)
            }else {
                self.openButton.setTitle("开通 VIP", for: .normal)
            }
            self.segmentedDataSource.titles = self.segmentTitles
            self.segmentedView.reloadData()
        } failure: { error in
            
        }
        
    }
}

extension HWVipCenterController {
    func addAction() -> Void {
        
    }
    
    @objc func openButtonClick(_ :UIButton) -> Void {
//        let webController = HWWebController()
//        webController.urlString = self.vipCenterModel.top.pay_url
//        self.navigationController?.pushViewController(webController, animated: true)
        self.navigationController?.pushViewController(HWVIPController(), animated: true)
    }
}
